package com.service;

import static com.def.GenericDef.EVT_MTRL_OUT;
import static com.def.GenericDef.E_BIS_MTRL;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_MTRL;
import static com.def.GenericDef.E_XPBISMTR_OTHER_ERR;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.SQL_OTHER_ERROR;
import static com.def.GenericDef.T_XPWHMTRL;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IBisMdlDefDao;
import com.dao.IBisMtrlDao;
import com.dao.IBisOpeDao;
import com.dao.IRetMtrlDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetWODao;
import com.dao.IRetWhMtrlDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Bis_mdl_def;
import com.model.Bis_mtrl;
import com.model.Bis_ope;
import com.model.Ret_mtrl;
import com.model.Ret_mtrlId;
import com.model.Ret_prd_in;
import com.model.Ret_wh_mtrl;
import com.model.Ret_wo;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xpwhmtrl.XpwhmtrlI;
import com.tx.Xpwhmtrl.XpwhmtrlIA;
import com.tx.Xpwhmtrl.XpwhmtrlO;
import com.tx.Xpwhmtrl.XpwhmtrlOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Service("XpwhmtrlService")
@Scope("prototype")
public class XpwhmtrlService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpwhmtrlService.class);
	private static String TX_NAME = T_XPWHMTRL;
	private XpwhmtrlI inTrx;
	private XpwhmtrlO xpwhmtrlO;
	
	@Autowired
	private IRetWhMtrlDao retWhMtrlDao;

	@Autowired
	private IRetMtrlDao retMtrlDao;
	
	@Autowired
	private IBisMtrlDao bisMtrlDao;
	
	@Autowired
	private IRetPrdInDao retPrdInDao;
	
	@Autowired
	private IBisDataDao bisDataDao;
	
	@Autowired
	private IRetWODao retWODao;
	
	@Autowired
	private IBisOpeDao bisOpeDao;
	
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	
	
	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		String reply ="";
		MqOpe mqOpe = new MqOpe();
		inTrx = new XpwhmtrlI();
		xpwhmtrlO = new XpwhmtrlO();
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		try{
			inTrx = (XpwhmtrlI) mqOpe.inStr2Obj(strInMsg,inTrx);
			xpwhmtrlO.setTrx_id("XPWHMTRL");
			xpwhmtrlO.setType_id("O");
			String actionFlg = inTrx.getAction_flg();
			if(actionFlg.equals("Q")){
				getWhMtrlProc();
			}else if( actionFlg.equals("A") ){
				addMtrlInfProc();
			}else if( actionFlg.equals("D") ){
				dedMtrlInfProc();
			}else if( actionFlg.equals("O") ){
				addOutMtrlInfProc();
			}else if( actionFlg.equals("S")){
				getOutMtrlInfProc();
			}else if( actionFlg.equals("B")){
				queryRetPrdIn();
			}else if( actionFlg.equals("C")){
				queryRetWhMtrl();
			}else if( actionFlg.equals("E")){
				changeStbShopWoId();
			}else if( actionFlg.equals("F")){
				changeDestShop();
			}else if( actionFlg.equals("X")){
				changeStbShopWoIdX();
			}else if( actionFlg.equals("Y")){
				changeDestShopY();
			}else if(actionFlg.equals("H")){
				getWhMtrl4700Proc();
			}else if(actionFlg.equals("K")){
				delRetMtrlProc();
			}else if(actionFlg.equals("I")){
				inqChgMtrlProc();
			}
			if( RETURN_CODE_OK.equals(xpwhmtrlO.getRtn_code()) ){
				retWhMtrlDao.flushCurSession();
				retMtrlDao.flushCurSession();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex ){
			logger.error(Util.stackTraceToString(ex));
			xpwhmtrlO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpwhmtrlO.setRtn_mesg(ex.toString());
		}finally{
			reply = mqOpe.obj2Str(xpwhmtrlO);
			logger.info(reply);
		}
		return reply;
		
	}
	public void getWhMtrlProc(){
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		try{
			XpwhmtrlIA iary = inTrx.getIary().get(0);
			String mtrl_prod_id = iary.getMtrl_prod_id_fk();
			String cus_id_fk = iary.getCus_id();
			String mtrl_cate = iary.getMtrl_cate();
			String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
			String dest_shop = iary.getDest_shop();
			String fabs = iary.getFabs();
			String sql = "SELECT A.* FROM RET_WH_MTRL A JOIN BIS_MTRL B ON A.MTRL_PROD_ID_FK=B.MTRL_PROD_ID WHERE 1=1";
			
	        if(!Util.isSpaceCheck(cus_id_fk)){
	        	sql = sql + " AND A.CUS_ID_FK='" + cus_id_fk + "'";
	        }
			if(!Util.isSpaceCheck(mtrl_cate)){
				sql = sql + " AND A.MTRL_CATE='" + mtrl_cate +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_id)){
				sql = sql + " AND A.MTRL_PROD_ID_FK='" + mtrl_prod_id +"'";
			}
			if(!Util.isSpaceCheck(dest_shop)){
				sql = sql + " AND A.DEST_SHOP='" + dest_shop +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_dsc)){
				sql = sql + " AND B.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
			}
			if (!Util.isSpaceCheck(fabs)) {
				sql += " AND A.INI_DEST_SHOP in ( SELECT DATA_ID from BIS_DATA where DATA_CATE = 'DEST' AND DATA_ITEM ='"+fabs+"')"; 
			}
			sql = sql + "  ORDER BY MTRL_PROD_ID_FK,DEST_SHOP,CUS_ID_FK,INWH_TIMESTAMP";

			List<Ret_wh_mtrl> mtrlList = retWhMtrlDao.findBySQL(sql, Ret_wh_mtrl.class);
			for(Ret_wh_mtrl ret_wh_mtrl:mtrlList){
				XpwhmtrlOA oary = new XpwhmtrlOA();
				oary.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
				oary.setMtrl_qty(ret_wh_mtrl.getMtrl_qty());
				oary.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
				oary.setEvt_timestamp(ret_wh_mtrl.getEvt_timestamp());
				oary.setInwh_timestamp(ret_wh_mtrl.getInwh_timestamp());
				oary.setEvt_usr(ret_wh_mtrl.getEvt_usr());
				oary.setDest_shop(ret_wh_mtrl.getDest_shop());
				oary.setIni_dest_shop(ret_wh_mtrl.getIni_dest_shop());
				oary.setCus_id(ret_wh_mtrl.getCus_id_fk());
				Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wh_mtrl.getMtrl_prod_id_fk());
				String hql = "FROM Bis_data where data_cate=? and data_item=?";
				Bis_data bis_data = bisDataDao.uniqueResult(hql, "CUSD",ret_wh_mtrl.getCus_id_fk());
				if(null!=bis_data){
					oary.setCus_dsc(bis_data.getData_ext());
				}else{
					oary.setCus_dsc(ret_wh_mtrl.getCus_id_fk());
				}
				oary.setMtrl_prod_dsc(bis_mtrl.getMtrl_prod_dsc());
				oary.setEvt_qty(ret_wh_mtrl.getEvt_qty());
				oary.setMtrl_seq_id(ret_wh_mtrl.getMtrl_seq_id());
				if(ret_wh_mtrl.getMtrl_evt_cate().equals("FLIN")){
					oary.setEvt_cate("辅料入库");
				}else if(ret_wh_mtrl.getMtrl_evt_cate().equals("FLNI")){
					oary.setEvt_cate("入库退料");
				}else if(ret_wh_mtrl.getMtrl_evt_cate().equals("CHGF")){
					oary.setEvt_cate("辅料转仓");
				}else if(ret_wh_mtrl.getMtrl_evt_cate().equals("CHGY")){
					oary.setEvt_cate("转仓收货");
				}else if(ret_wh_mtrl.getMtrl_evt_cate().equals("FLOT")){
					oary.setEvt_cate("辅料发料");
				}else if(ret_wh_mtrl.getMtrl_evt_cate().equals("FLTO")){
					oary.setEvt_cate("发料退料");
				}else{
					oary.setEvt_cate(ret_wh_mtrl.getMtrl_evt_cate());
				}
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
	private void addMtrlInfProc() {
		String mtrl_prod_id="",evt_user="",dest_shop="",cus_id="";
		mtrl_prod_id = inTrx.getIary().get(0).getMtrl_prod_id_fk();
		evt_user = inTrx.getEvt_usr();
		dest_shop = inTrx.getIary().get(0).getDest_shop();
		cus_id = inTrx.getIary().get(0).getCus_id();
		double evt_qty = Double.valueOf( inTrx.getIary().get(0).getEvt_qty() );
		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
		if( bis_mtrl == null){
			gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}
		
		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
		
		Ret_wh_mtrl ret_wh_mtrl = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, mtrl_prod_id,dest_shop,cus_id);
		if( ret_wh_mtrl != null){
			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
			ret_wh_mtrl_new.setMtrl_qty(ret_wh_mtrl.getMtrl_qty()+evt_qty);
			ret_wh_mtrl_new.setEvt_qty(evt_qty);
			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
			ret_wh_mtrl_new.setDest_shop(dest_shop);
			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
			ret_wh_mtrl_new.setEvt_cate("FLIN");//辅料入库
			ret_wh_mtrl_new.setMtrl_evt_cate("FLIN");
			ret_wh_mtrl_new.setEvt_usr(evt_user);
			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
			ret_wh_mtrl_new.setCus_id_fk(cus_id);
			retWhMtrlDao.save(ret_wh_mtrl_new);
			ret_wh_mtrl.setMtrl_qty(null );
			retWhMtrlDao.update(ret_wh_mtrl);
		}else{
			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
			ret_wh_mtrl_new.setMtrl_qty(evt_qty);
			ret_wh_mtrl_new.setEvt_qty(evt_qty);
			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
			ret_wh_mtrl_new.setEvt_cate("FLIN");//辅料入库
			ret_wh_mtrl_new.setMtrl_evt_cate("FLIN");
			ret_wh_mtrl_new.setDest_shop(dest_shop);
			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
			ret_wh_mtrl_new.setEvt_usr(evt_user);
			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
			ret_wh_mtrl_new.setCus_id_fk(cus_id);
			retWhMtrlDao.save(ret_wh_mtrl_new);
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	private void getOutMtrlInfProc() {
		XpwhmtrlIA iary = inTrx.getIary().get(0);
		List<Ret_mtrl> ret_mtrl_list = new ArrayList<Ret_mtrl>();
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		String mtrl_prod_id="",
				   evt_usr="",
				   ope_id ="",
				   sql="",
				   mtrl_cate="",
				   dest_shop="",
				   mtrl_prod_dsc="";
		String fabs = iary.getFabs();
		try{
			mtrl_prod_id = iary.getMtrl_prod_id_fk();
			ope_id = iary.getOpe_id_fk();
			mtrl_prod_dsc = iary.getMtrl_prod_dsc();
			dest_shop = iary.getDest_shop();
			mtrl_cate = iary.getMtrl_cate();
			Timestamp from_date = iary.getFrom_date();
			Timestamp to_date = iary.getTo_date();
			
		    sql = "SELECT A.* FROM RET_MTRL A JOIN BIS_MTRL B ON A.MTRL_PROD_ID_FK=B.MTRL_PROD_ID WHERE 1=1";
			
			if(!Util.isSpaceCheck(mtrl_cate)){
				sql = sql + " AND A.MTRL_CATE='" + mtrl_cate +"'";
			}
			if(!Util.isSpaceCheck(ope_id)){
				sql = sql + " AND A.OPE_ID_FK='" + ope_id +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_id)){
				sql = sql + " AND A.MTRL_PROD_ID_FK='" + mtrl_prod_id +"'";
			}
			if(!Util.isSpaceCheck(dest_shop)){
				sql = sql + " AND A.DEST_SHOP='" + dest_shop +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_dsc)){
				sql = sql + " AND B.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
			}
			if(null != from_date){
				sql = sql + " AND A.EVT_TIMESTAMP>'" + from_date + "'";
			}
			if(null != to_date){
				sql = sql + " AND A.EVT_TIMESTAMP<'" + to_date + "'";
			}
			if(null != iary.getRelate_usr()){
				sql = sql + " AND A.RELATE_USR='" + iary.getRelate_usr() + "'"; 
			}
			if (!Util.isSpaceCheck(fabs)) {
				sql += " AND A.DEST_SHOP in ( SELECT DATA_ID from BIS_DATA where DATA_CATE = 'DEST' AND DATA_ITEM ='"+fabs+"')"; 
			}
			List<Ret_mtrl> mtrlist = retMtrlDao.findBySQL(sql, Ret_mtrl.class);

			
			for(Ret_mtrl ret_mtrl : mtrlist){
				XpwhmtrlOA oary = new XpwhmtrlOA();
				oary.setMtrl_prod_id_fk(ret_mtrl.getMtrl_prod_id_fk());
				oary.setMtrl_cate(ret_mtrl.getMtrl_cate());
				oary.setMtrl_qty(ret_mtrl.getMtrl_qty());
				oary.setEvt_usr(ret_mtrl.getEvt_usr());
				oary.setOpe_id_fk(ret_mtrl.getOpe_id_fk());
				oary.setRelate_usr(ret_mtrl.getRelate_usr());
				oary.setEvt_timestamp(ret_mtrl.getEvt_timestamp());
				Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_mtrl.getMtrl_prod_id_fk());
				String hql = "FROM Bis_data where data_cate=? and data_item=?";
				Bis_data bis_data = bisDataDao.uniqueResult(hql, "CUSD",ret_mtrl.getCus_id_fk());
				if(null!=bis_data){
					oary.setCus_dsc(bis_data.getData_ext());
				}else{
					oary.setCus_dsc(ret_mtrl.getCus_id_fk());
				}
				String hqlope = "From Bis_ope where ope_id=? and ope_ver=?";
				Bis_ope bis_ope = bisOpeDao.uniqueResult(hqlope,ret_mtrl.getOpe_id_fk(),"00000");
				if(null!=bis_ope){
					oary.setOpe_dsc(bis_ope.getOpe_dsc());
				}else{
					oary.setOpe_dsc(ret_mtrl.getOpe_id_fk());
				}
				oary.setMtrl_prod_dsc(bis_mtrl.getMtrl_prod_dsc());
				oary.setCus_id(ret_mtrl.getCus_id_fk());
				oary.setDest_shop(ret_mtrl.getDest_shop());
				oary.setMtrl_seq_id(ret_mtrl.getMtrl_seq_id());
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
	private void addOutMtrlInfProc() {
		XpwhmtrlIA iary = inTrx.getIary().get(0);
		String mtrl_prod_id="",
			   evt_usr="",
			   ope_id ="",
			   relate_usr="",
			   cus_id="",
			   dest_shop="";
		double wh_mtrl_qty =0.0;
		double mtrl_qty = Double.valueOf(iary.getEvt_qty());
		mtrl_prod_id = iary.getMtrl_prod_id_fk();
		evt_usr = inTrx.getEvt_usr();
		relate_usr = inTrx.getRelate_usr();
		ope_id = iary.getOpe_id_fk();
		cus_id = iary.getCus_id();
		dest_shop = iary.getDest_shop();
		
		String sql = "SELECT * FROM RET_WH_MTRL WHERE MTRL_PROD_ID_FK='"
				     + mtrl_prod_id + "' AND DEST_SHOP='"
				     + dest_shop +"' AND INWH_TIMESTAMP<>'null' ORDER BY INWH_TIMESTAMP";
		List<Ret_wh_mtrl> mtrlist = retWhMtrlDao.findBySQL(sql, Ret_wh_mtrl.class);
        for(int i=0 ;i<mtrlist.size();i++){
        	wh_mtrl_qty = wh_mtrl_qty + mtrlist.get(i).getEvt_qty();
        }
		
		if(wh_mtrl_qty<mtrl_qty){
			gszMsgBuf = "物料["+mtrl_prod_id+"]库存["+wh_mtrl_qty+"]不够,无法发料，请先做物料入库！";
			xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}
		
        for(Ret_wh_mtrl ret_wh_mtrl:mtrlist){
        	if(ret_wh_mtrl.getEvt_qty()>mtrl_qty){
        		ret_wh_mtrl.setEvt_qty(ret_wh_mtrl.getEvt_qty() - mtrl_qty);
        		ret_wh_mtrl.setEvt_usr(evt_usr);
        		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
        		ret_wh_mtrl.setEvt_cate("FLOT");
        		retWhMtrlDao.update(ret_wh_mtrl);
        		
        		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
        		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
        	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - mtrl_qty);
        	    retWhMtrlDao.update(ret_wh_mtrl_all);
        	    
//        	    String hql1 = "FROM Ret_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND OPE_ID_FK=?";
//        	    Ret_mtrl ret_mtrl =(Ret_mtrl) retMtrlDao.uniqueResult(hql1,ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk(), ope_id);
//                if( ret_mtrl == null){
        			Ret_mtrl ret_mtrl_new = new Ret_mtrl();
        			ret_mtrl_new.setMtrl_seq_id( GUIDGenerator.javaGUID());
        			ret_mtrl_new.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
        			ret_mtrl_new.setOpe_id_fk(ope_id);
        			ret_mtrl_new.setEvt_usr(evt_usr);
        			ret_mtrl_new.setRelate_usr(relate_usr);
        			ret_mtrl_new.setMtrl_qty(mtrl_qty);
        			ret_mtrl_new.setDest_shop(ret_wh_mtrl.getDest_shop());
        			ret_mtrl_new.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
        			ret_mtrl_new.setEvt_timestamp(gEvtTimestamp);
        			ret_mtrl_new.setEvt_cate("FLOT");
        			ret_mtrl_new.setMtrl_evt_cate("FLOT");
        			ret_mtrl_new.setCus_id_fk(ret_wh_mtrl.getCus_id_fk());
        			retMtrlDao.save(ret_mtrl_new);
//                }else{
//        			ret_mtrl.setEvt_cate(EVT_MTRL_OUT);
//        			ret_mtrl.setEvt_usr(evt_usr);
//        			ret_mtrl.setRelate_usr(relate_usr);
//        			ret_mtrl.setMtrl_qty(ret_mtrl.getMtrl_qty() + mtrl_qty);
//        			ret_mtrl.setEvt_timestamp(gEvtTimestamp);
//        			retMtrlDao.update(ret_mtrl);
//                } 
                break;
        	}else{
          		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
        		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
        	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - ret_wh_mtrl.getEvt_qty());
        	    retWhMtrlDao.update(ret_wh_mtrl_all);
        		ret_wh_mtrl.setEvt_usr(evt_usr);
        		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
        		ret_wh_mtrl.setEvt_cate("FLOT");
        	    retWhMtrlDao.delete(ret_wh_mtrl);
        	    
//        	    String hql1 = "FROM Ret_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND OPE_ID_FK=?";
//        	    Ret_mtrl ret_mtrl =(Ret_mtrl) retMtrlDao.uniqueResult(hql1,ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk(), ope_id);
//                if( ret_mtrl == null){
        			Ret_mtrl ret_mtrl_new = new Ret_mtrl();
        			ret_mtrl_new.setMtrl_seq_id( GUIDGenerator.javaGUID());
        			ret_mtrl_new.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
        			ret_mtrl_new.setOpe_id_fk(ope_id);
        			ret_mtrl_new.setEvt_usr(evt_usr);
        			ret_mtrl_new.setRelate_usr(relate_usr);
        			ret_mtrl_new.setMtrl_qty(ret_wh_mtrl.getEvt_qty());
        			ret_mtrl_new.setDest_shop(ret_wh_mtrl.getDest_shop());
        			ret_mtrl_new.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
        			ret_mtrl_new.setEvt_timestamp(gEvtTimestamp);
        			ret_mtrl_new.setEvt_cate("FLOT");
        			ret_mtrl_new.setMtrl_evt_cate("FLOT");
        			ret_mtrl_new.setCus_id_fk(ret_wh_mtrl.getCus_id_fk());
        			retMtrlDao.save(ret_mtrl_new);
//                }else{
//        			ret_mtrl.setEvt_cate(EVT_MTRL_OUT);
//        			ret_mtrl.setEvt_usr(evt_usr);
//        			ret_mtrl.setRelate_usr(relate_usr);
//        			ret_mtrl.setMtrl_qty(ret_mtrl.getMtrl_qty() + ret_wh_mtrl.getEvt_qty());
//        			ret_mtrl.setEvt_timestamp(gEvtTimestamp);
//        			retMtrlDao.update(ret_mtrl);
//                }
                mtrl_qty = mtrl_qty - ret_wh_mtrl.getEvt_qty();
                if(mtrl_qty==0){
                	break;
                }
        	}
        }

		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	private void dedMtrlInfProc() {
		// TODO Auto-generated method stub
		String mtrl_prod_id="",mtrl_seq_id="",evt_user="";
		mtrl_seq_id = inTrx.getIary().get(0).getMtrl_seq_id();
		mtrl_prod_id = inTrx.getIary().get(0).getMtrl_prod_id_fk();
		evt_user = inTrx.getEvt_usr();
		double mtrl_qty = Double.valueOf( inTrx.getIary().get(0).getEvt_qty() );
		Ret_wh_mtrl ret_wh_mtrl = retWhMtrlDao.get(mtrl_seq_id);
		if( ret_wh_mtrl == null){
			gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做过入库,无法做异常退料！";
			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}
		if("CHGF".equals(ret_wh_mtrl.getMtrl_note())){
			gszMsgBuf = "此物料["+mtrl_prod_id+"]正在转仓。请先收料";
			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}
		if( mtrl_qty > ret_wh_mtrl.getEvt_qty()){
			gszMsgBuf = "物料["+mtrl_prod_id+"]退料的数量大于库存，无法退料";
			xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}else if( mtrl_qty == ret_wh_mtrl.getEvt_qty()){
           if(null != ret_wh_mtrl.getMtrl_qty()){
    	    	String sqlnew = " SELECT * FROM RET_WH_MTRL WHERE MTRL_PROD_ID_FK='"
    	    			         + ret_wh_mtrl.getMtrl_prod_id_fk() + "' AND DEST_SHOP='"
    	    			         + ret_wh_mtrl.getDest_shop() + "' AND CUS_ID_FK='"
    	    			         + ret_wh_mtrl.getCus_id_fk() +"' AND INWH_TIMESTAMP<>'null' ORDER BY INWH_TIMESTAMP DESC";
    	    	List<Ret_wh_mtrl> retwhlist = retWhMtrlDao.findBySQL(sqlnew, Ret_wh_mtrl.class);
    	    	if(retwhlist.size()>1){
    	    		Ret_wh_mtrl ret_wh_mtrl_all_new = retwhlist.get(1);
               		ret_wh_mtrl_all_new.setMtrl_qty(ret_wh_mtrl.getMtrl_qty() - mtrl_qty);
            		retWhMtrlDao.update(ret_wh_mtrl_all_new);
            		ret_wh_mtrl.setEvt_cate("FLNI");//辅料入库退料
            		ret_wh_mtrl.setEvt_usr(evt_user);
            		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
            		retWhMtrlDao.delete(ret_wh_mtrl);
    	    	}else if(retwhlist.size()==1){
    	    		ret_wh_mtrl.setEvt_cate("FLNI");//辅料入库退料
    	    		ret_wh_mtrl.setEvt_usr(evt_user);
            		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
    	    		retWhMtrlDao.delete(ret_wh_mtrl);
    	    	}
           }else{
           		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
        		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
        	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - mtrl_qty);
        	    retWhMtrlDao.update(ret_wh_mtrl_all);
        	    ret_wh_mtrl.setEvt_cate("FLNI");//辅料入库退料
        		ret_wh_mtrl.setEvt_usr(evt_user);
        		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
        	    retWhMtrlDao.delete(ret_wh_mtrl);
    	    }
        }else{
       		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
    		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
    	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - mtrl_qty);
    	    retWhMtrlDao.update(ret_wh_mtrl_all);
    	    ret_wh_mtrl.setEvt_cate("FLNI");//辅料入库退料
    		ret_wh_mtrl.setEvt_usr(evt_user);
    		ret_wh_mtrl.setEvt_qty(ret_wh_mtrl.getEvt_qty() - mtrl_qty);
    		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
    		retWhMtrlDao.update(ret_wh_mtrl);
        }

		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	/**
	 * 查询ret_prd_in
	 * @author:Haoyuan.Tian
	 * date:2014-11-15
	 */
	private void queryRetPrdIn() {
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		try{
			String cus_id = inTrx.getCus_id();
			String mtrl_prod_id_fk = inTrx.getMtrl_prod_id_fk();
			String stb_shop = inTrx.getStb_shop();
			String wo_id_fk = inTrx.getWo_id_fk();
			String mtrl_box_id = inTrx.getMtrl_box_id();
			String evtcate = inTrx.getEvt_cate();
			String stb_shop1="";
			String fabs = inTrx.getFabs();
			
			String sql = "SELECT * FROM RET_PRD_IN where 1=1 AND ppbox_stat='WAIT'";
			if(mtrl_box_id != null){
				sql += " AND mtrl_box_id = '" + mtrl_box_id + "'";
			}else {
				if(cus_id != null){
					sql += " AND cus_id = '" + cus_id + "'";
				}
				if(mtrl_prod_id_fk != null){
					sql += " AND mtrl_prod_id_fk = '" + mtrl_prod_id_fk + "'";
				}
				if(stb_shop != null){
					sql += " AND stb_shop = '" + stb_shop + "'";
				}
				if(wo_id_fk != null){
					sql += " AND wo_id_fk = '" + wo_id_fk + "'";
				}
				if(evtcate !=null){
					sql += " AND evt_cate = '" + evtcate + "'";
				}else{
					sql += " AND evt_cate<>'CHGE'";
				}
			}
			
			List<Ret_prd_in> retPrdInList = retPrdInDao.findBySQL(sql, Ret_prd_in.class);
			for(Ret_prd_in ret_prd_in : retPrdInList){
				XpwhmtrlOA oary = new XpwhmtrlOA();
				oary.setCus_id(ret_prd_in.getId().getCus_id());
				oary.setMtrl_box_id(ret_prd_in.getId().getMtrl_box_id());
				oary.setMtrl_prod_id_fk(ret_prd_in.getMtrl_prod_id_fk());
				oary.setPrd_tl_qty(ret_prd_in.getPrd_tl_qty().toString());
				oary.setStb_shop(ret_prd_in.getStb_shop());
				oary.setWo_id_fk(ret_prd_in.getWo_id_fk());
				stb_shop1=ret_prd_in.getStb_shop();
				if(evtcate != null){
					stb_shop1=new StringBuilder(ret_prd_in.getStb_shop()).reverse().toString(); 
					oary.setStb_shop(stb_shop1);
				}
				
				String hql = "FROM Bis_data WHERE data_cate = 'DEST' AND data_ext in ('C','D') AND data_id = '" + stb_shop1 + "'"; 
				List<Bis_data> dataList = bisDataDao.find(hql);
				if(dataList.size() == 0){
					gszMsgBuf = "箱子["+ret_prd_in.getId().getMtrl_box_id()+"]仓别["+stb_shop1+"]在bis_data中不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				if(stb_shop==null&&!dataList.get(0).getData_item().equals(fabs)){
					continue;
				}
				if(evtcate != null){
					oary.setDes_data_desc(dataList.get(0).getData_desc());
				}else{
					oary.setData_desc(dataList.get(0).getData_desc());
				}
			    
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
	
	/**
	 * 查询ret_wh_mtrl
	 * @author:Haoyuan.Tian
	 * date:2014-11-15
	 */
	private void queryRetWhMtrl() {
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		try{
			String cus_id = inTrx.getCus_id();
			String mtrl_prod_id_fk = inTrx.getMtrl_prod_id_fk();
			String stb_shop = inTrx.getStb_shop();
			String mtrl_lot_id = inTrx.getMtrl_box_id();
			String evtcate = inTrx.getEvt_cate();
			String dest_shop1="";
			String sql = "FROM Ret_wh_mtrl where 1=1 ";
			
			if(mtrl_lot_id != null){
				sql += " AND mtrl_lot_id = '" + mtrl_lot_id + "'";
			}else {
				if(cus_id != null){
					sql += " AND cus_id_fk = '" + cus_id + "'";
				}
				if(mtrl_prod_id_fk != null){
					sql += " AND mtrl_prod_id_fk = '" + mtrl_prod_id_fk + "'";
				}
				if(stb_shop != null){
					sql += " AND dest_shop = '" + stb_shop + "'";
				}
				if(evtcate !=null){
					sql += " AND evt_cate = '" + evtcate + "'";
				}
			}
			
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(sql);
			
			for(Ret_wh_mtrl ret_wh_mtrl : retWhMtrlList){
				XpwhmtrlOA oary = new XpwhmtrlOA();
				
				oary.setCus_id(ret_wh_mtrl.getCus_id_fk());
				oary.setMtrl_box_id(ret_wh_mtrl.getMtrl_lot_id());
				oary.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
				oary.setPrd_tl_qty(ret_wh_mtrl.getMtrl_qty().toString());
				oary.setStb_shop(ret_wh_mtrl.getDest_shop());
				dest_shop1=ret_wh_mtrl.getDest_shop();
				if(evtcate != null){
					dest_shop1=new StringBuilder(ret_wh_mtrl.getDest_shop()).reverse().toString(); 
					oary.setStb_shop(dest_shop1);
				}
				String hql = "FROM Bis_data WHERE data_cate = 'DEST' AND data_ext = 'A' AND data_id = '" + dest_shop1 + "'"; 
				List<Bis_data> dataList = bisDataDao.find(hql);
				if(dataList.size() == 0){
					gszMsgBuf = "箱子["+ret_wh_mtrl.getMtrl_lot_id()+"]仓别["+ dest_shop1 +"]在bis_data中不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				
				if(evtcate != null){
					oary.setDes_data_desc(dataList.get(0).getData_desc());
				}else{
					oary.setData_desc(dataList.get(0).getData_desc());
				}
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
	
	/**
	 * 改变原料仓别
	 * @author:Haoyuan.Tian
	 * date:2014-11-15
	 */
	private void changeStbShopWoId() {
		
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		List<XpwhmtrlIA> iary = inTrx.getIary();
		String wo_id_fk = inTrx.getWo_id_fk();
		String evtusr = inTrx.getEvt_usr();
		String force_flg = "N";
		String wo_id_fk_old = "";
		int iChgqty = 0, has_mdlID_O = 0, has_mdlID_N = 0;
		Ret_wo ret_wo_new = new Ret_wo();
		Ret_wo ret_wo_old = new Ret_wo();
	
		try {
			
			if(!Util.isSpaceCheck(wo_id_fk)){
				Bis_mdl_def bismdl_new = new Bis_mdl_def();
				Bis_mdl_def bismdl_old = new Bis_mdl_def();
				ret_wo_new = retWODao.get(wo_id_fk);
				
				if (ret_wo_new == null) {
					gszMsgBuf = "转入工单["+wo_id_fk+"]不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				if ( ret_wo_new.getMdl_id_fk() != null && !Util.isSpaceCheck(ret_wo_new.getMdl_id_fk()) )  {
					has_mdlID_N = 1;
					bismdl_new = bisMdlDefDao.get(ret_wo_new.getMdl_id_fk());
					
				}
				
				String sql = "FROM Ret_prd_in where mtrl_box_id = '"+ iary.get(0).getMtrl_box_id() + "'";;				
				List<Ret_prd_in> retPrdInList = retPrdInDao.find(sql);
				if(retPrdInList.isEmpty()){
					gszMsgBuf = "该箱号不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				ret_wo_old = retWODao.get(retPrdInList.get(0).getWo_id_fk());
				if (ret_wo_old == null) {
					gszMsgBuf = "工单["+retPrdInList.get(0).getWo_id_fk()+"]不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				if ( ret_wo_old.getMdl_id_fk() != null && !Util.isSpaceCheck(ret_wo_old.getMdl_id_fk()) )  {
					has_mdlID_O = 1;
					bismdl_old = bisMdlDefDao.get(ret_wo_old.getMdl_id_fk());
					
				}
				if ( has_mdlID_O == 1 && has_mdlID_N == 1 ){
					if ( !bismdl_old.getLayot_id_fk().equals(bismdl_new.getLayot_id_fk()) ) {
						gszMsgBuf = "版式不同，不能转WO";
						xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
						xpwhmtrlO.setRtn_mesg(gszMsgBuf);
						return;
					}
					
				}
			}
			
			for (int i=0;i<iary.size();i++){
				String sql = "FROM Ret_prd_in where 1 = 1 ";
				String stb_shop = iary.get(i).getStb_shop();
				String new_stb_shop = new StringBuilder(stb_shop).reverse().toString(); 
				String mtrl_box_id = iary.get(i).getMtrl_box_id();
				if (Util.isSpaceCheck(stb_shop)){
					gszMsgBuf = "转入仓别不能为空";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				if (Util.isSpaceCheck(mtrl_box_id)){
					gszMsgBuf = "选中的记录中箱号/批次为空，请确认";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}else {
					sql += "AND mtrl_box_id = '" + mtrl_box_id + "'";
				}
				
				List<Ret_prd_in> retPrdInList = retPrdInDao.find(sql);
				if(retPrdInList.isEmpty()){
					gszMsgBuf = "该箱号不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}else{
					Ret_prd_in ret_prd_in = retPrdInList.get(0);
					
					if(Util.isSpaceCheck(wo_id_fk)&&ret_prd_in.getStb_shop().equals(stb_shop)){
						gszMsgBuf = "原仓位与转入仓位一致，不能转仓";
						xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
						xpwhmtrlO.setRtn_mesg(gszMsgBuf);
						return;
					}
                    if(ret_prd_in.getEvt_cate().equals("CHGE")){
						gszMsgBuf = "转仓中箱子需先做收货动作！";
						xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
						xpwhmtrlO.setRtn_mesg(gszMsgBuf);
						return;
                    }
					
					if(!Util.isSpaceCheck(wo_id_fk)){
						if (!ret_prd_in.getPpbox_stat().equals("WAIT")) {
							gszMsgBuf = "此箱["+mtrl_box_id+"]必须为WAIT状态，即4100来料未发料，不能转工单";
							xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
							xpwhmtrlO.setRtn_mesg(gszMsgBuf);
							return;
						}
						if(ret_prd_in.getPrd_proc_qty() != 0){
							gszMsgBuf = "此箱["+mtrl_box_id+"]已下线，不能转工单";
							xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
							xpwhmtrlO.setRtn_mesg(gszMsgBuf);
							return;
						}else {
							wo_id_fk_old = ret_prd_in.getWo_id_fk();
							
							Ret_wo ret_wo = retWODao.get(wo_id_fk_old);
							if (ret_wo == null) {
								gszMsgBuf = "工单["+wo_id_fk_old+"]不存在";
								xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
								xpwhmtrlO.setRtn_mesg(gszMsgBuf);
								return;
							}
							if(ret_prd_in.getStb_shop().equals(stb_shop)){
								ret_prd_in.setEvt_timestamp(gEvtTimestamp);
								ret_prd_in.setEvt_usr(evtusr);
							}
							iChgqty += ret_prd_in.getPrd_tl_qty();
							ret_wo.setPln_prd_qty(ret_wo.getPln_prd_qty() - ret_prd_in.getPrd_tl_qty());
							retWODao.update(ret_wo);
							
							ret_prd_in.setWo_id_fk(wo_id_fk);
							ret_prd_in.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
						}
					}else{
						ret_prd_in.setStb_shop(new_stb_shop);
						ret_prd_in.setEvt_cate("CHGE");
						ret_prd_in.setEvt_timestamp(gEvtTimestamp);
						ret_prd_in.setEvt_usr(evtusr);
					}

					retPrdInDao.update(ret_prd_in);
				}
			}
			
			if(!Util.isSpaceCheck(wo_id_fk)){
				Ret_wo ret_wo = retWODao.get(wo_id_fk);
				
				if (ret_wo == null) {
					gszMsgBuf = "转入工单["+wo_id_fk+"]不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				
				int pln_prd_qty = ret_wo.getPln_prd_qty();
				ret_wo.setPln_prd_qty(pln_prd_qty + iChgqty);
				
				retWODao.update(ret_wo);
				
				
			}
			
		} catch (Exception ex) {
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	/**
	 * 改变辅料仓别
	 * @author:wfq
	 * date:2015-08-09
	 */
	private void changeDestShop() {
		
		List<XpwhmtrlIA> iary = inTrx.getIary();
		String evt_user="",stb_shop="";
		evt_user = inTrx.getEvt_usr();
		stb_shop = inTrx.getStb_shop();
		String new_dest_shop = new StringBuilder(stb_shop).reverse().toString();
		if(!Util.isSpaceCheck(inTrx.getEvt_qty())){
			double mtrl_qty = Double.valueOf( inTrx.getEvt_qty() );
			String mtrl_seq_id = iary.get(0).getMtrl_seq_id();
			String mtrl_prod_id = iary.get(0).getMtrl_prod_id_fk();
			Ret_wh_mtrl ret_wh_mtrl = retWhMtrlDao.get(mtrl_seq_id);
			if( ret_wh_mtrl == null){
				gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做过入库,无法做异常退料！";
				xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
				xpwhmtrlO.setRtn_mesg(gszMsgBuf);
				return;
			}
			if("CHGF".equals(ret_wh_mtrl.getMtrl_note())){
				gszMsgBuf = "此物料["+mtrl_prod_id+"]正在转仓。请先收料";
				xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
				xpwhmtrlO.setRtn_mesg(gszMsgBuf);
				return;
			}
			Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
			if( bis_mtrl == null){
				gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
				xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
				xpwhmtrlO.setRtn_mesg(gszMsgBuf);
				return;
			}
	        String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
	    		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
	    	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - mtrl_qty);
	    	    retWhMtrlDao.update(ret_wh_mtrl_all);
	    	    ret_wh_mtrl.setEvt_cate("CHGF");//辅料转仓
	    		ret_wh_mtrl.setEvt_usr(evt_user);
	    		ret_wh_mtrl.setEvt_qty(ret_wh_mtrl.getEvt_qty() - mtrl_qty);
	    		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
	    		retWhMtrlDao.update(ret_wh_mtrl);
				Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
				ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
				ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
				ret_wh_mtrl_new.setEvt_qty(mtrl_qty);
				ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
				ret_wh_mtrl_new.setEvt_cate("CHGF");//辅料转仓
				ret_wh_mtrl_new.setMtrl_note("CHGF");
				ret_wh_mtrl_new.setMtrl_evt_cate("CHGF");
				ret_wh_mtrl_new.setIni_dest_shop(ret_wh_mtrl.getDest_shop());
				ret_wh_mtrl_new.setDest_shop(new_dest_shop);
				ret_wh_mtrl_new.setEvt_usr(evt_user);
				ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
				ret_wh_mtrl_new.setCus_id_fk(ret_wh_mtrl.getCus_id_fk());
				retWhMtrlDao.save(ret_wh_mtrl_new);
		}else{
			for (int i=0;i<iary.size();i++){
				String mtrl_seq_id = iary.get(i).getMtrl_seq_id();
				String mtrl_prod_id = iary.get(i).getMtrl_prod_id_fk();
				Ret_wh_mtrl ret_wh_mtrl = retWhMtrlDao.get(mtrl_seq_id);
				if( ret_wh_mtrl == null){
					gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做过入库,无法做异常退料！";
					xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
				if( bis_mtrl == null){
					gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
					xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
		           if(null != ret_wh_mtrl.getMtrl_qty()){
		    	    	String sqlnew = " SELECT * FROM RET_WH_MTRL WHERE MTRL_PROD_ID_FK='"
		    	    			         + ret_wh_mtrl.getMtrl_prod_id_fk() + "' AND DEST_SHOP='"
		    	    			         + ret_wh_mtrl.getDest_shop() + "' AND CUS_ID_FK='"
		    	    			         + ret_wh_mtrl.getCus_id_fk() +"' AND INWH_TIMESTAMP<>'null' ORDER BY INWH_TIMESTAMP DESC";
		    	    	List<Ret_wh_mtrl> retwhlist = retWhMtrlDao.findBySQL(sqlnew, Ret_wh_mtrl.class);
		    	    	if(retwhlist.size()>1){
		    	    		Ret_wh_mtrl ret_wh_mtrl_all_new = retwhlist.get(1);
		               		ret_wh_mtrl_all_new.setMtrl_qty(ret_wh_mtrl.getMtrl_qty() - ret_wh_mtrl.getEvt_qty());
		            		retWhMtrlDao.update(ret_wh_mtrl_all_new);
		            		ret_wh_mtrl.setEvt_cate("CHGF");//辅料转仓
		            		ret_wh_mtrl.setEvt_usr(evt_user);
		            		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
		            		retWhMtrlDao.delete(ret_wh_mtrl);
		    	    	}else if(retwhlist.size()==1){
		    	    		ret_wh_mtrl.setEvt_cate("CHGF");//辅料转仓
		    	    		ret_wh_mtrl.setEvt_usr(evt_user);
		            		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
		    	    		retWhMtrlDao.delete(ret_wh_mtrl);
		    	    	}
		           }else{
		           		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
		        		Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, ret_wh_mtrl.getMtrl_prod_id_fk(),ret_wh_mtrl.getDest_shop(),ret_wh_mtrl.getCus_id_fk());
		        	    ret_wh_mtrl_all.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty() - ret_wh_mtrl.getEvt_qty());
		        	    retWhMtrlDao.update(ret_wh_mtrl_all);
		        	    ret_wh_mtrl.setEvt_cate("CHGF");//辅料转仓
		        		ret_wh_mtrl.setEvt_usr(evt_user);
		        		ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
		        	    retWhMtrlDao.delete(ret_wh_mtrl);
		    	    }
					Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
					ret_wh_mtrl_new.setMtrl_seq_id(GUIDGenerator.javaGUID());
					ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
					ret_wh_mtrl_new.setEvt_qty(ret_wh_mtrl.getEvt_qty());
					ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
					ret_wh_mtrl_new.setEvt_cate("CHGF");//辅料转仓
					ret_wh_mtrl_new.setMtrl_note("CHGF");
					ret_wh_mtrl_new.setMtrl_evt_cate("CHGF");
					ret_wh_mtrl_new.setIni_dest_shop(ret_wh_mtrl.getDest_shop());
					ret_wh_mtrl_new.setDest_shop(new_dest_shop);
					ret_wh_mtrl_new.setEvt_usr(evt_user);
					ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
					ret_wh_mtrl_new.setCus_id_fk(ret_wh_mtrl.getCus_id_fk());
					retWhMtrlDao.save(ret_wh_mtrl_new);
			}
		}
	    
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	/**
	 * 原料仓别收货
	 * @author:wfq
	 * date:2015-03-20
	 */
	private void changeStbShopWoIdX() {
		
		List<XpwhmtrlIA> iary = inTrx.getIary();
		String evtusr = inTrx.getEvt_usr();

		try {
			for (int i=0;i<iary.size();i++){
				String sql = "FROM Ret_prd_in where 1 = 1 ";
				String mtrl_box_id = iary.get(i).getMtrl_box_id();
				if (Util.isSpaceCheck(mtrl_box_id)){
					gszMsgBuf = "选中的记录中箱号/批次为空，请确认";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}else {
					sql += "AND mtrl_box_id = '" + mtrl_box_id + "'";
				}
				
				List<Ret_prd_in> retPrdInList = retPrdInDao.find(sql);
				if(retPrdInList.isEmpty()){
					gszMsgBuf = "该箱号不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}else{
					Ret_prd_in ret_prd_in = retPrdInList.get(0);
					ret_prd_in.setStb_shop(new StringBuilder(ret_prd_in.getStb_shop()).reverse().toString());
					ret_prd_in.setEvt_cate("CHGX");
					ret_prd_in.setEvt_timestamp(gEvtTimestamp);
					ret_prd_in.setEvt_usr(evtusr);
					
					retPrdInDao.update(ret_prd_in);
				}
			}
		} catch (Exception ex) {
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	/**
	 * 辅料仓别收货
	 * @author:wfq
	 * date:2015-03-20
	 */
	private void changeDestShopY() {
		List<XpwhmtrlIA> iary = inTrx.getIary();
		String evt_user="";
		evt_user = inTrx.getEvt_usr();
		String fabs = inTrx.getFabs();
			for (int i=0;i<iary.size();i++){
				String mtrl_seq_id = iary.get(i).getMtrl_seq_id();
				String mtrl_prod_id = iary.get(i).getMtrl_prod_id_fk();
				Ret_wh_mtrl ret_wh_mtrl = retWhMtrlDao.get(mtrl_seq_id);
				if( ret_wh_mtrl == null){
					gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做过入库,无法做异常退料！";
					xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
				if( bis_mtrl == null){
					gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
					xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				String dest_shop=new StringBuilder(ret_wh_mtrl.getDest_shop()).reverse().toString(); 
				
				String hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_ext in ('A') AND data_id = '" + dest_shop + "'"; 
				List<Bis_data> dataList = bisDataDao.find(hqldest);
				if(dataList.size() == 0){
					gszMsgBuf = "箱子["+mtrl_prod_id+"]仓别["+dest_shop+"]在bis_data中不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				if(!fabs.equals(dataList.get(0).getData_item())){
					gszMsgBuf = "没有权限对此箱收货";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				
				String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
				
				Ret_wh_mtrl ret_wh_mtrl_all = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, mtrl_prod_id,dest_shop,ret_wh_mtrl.getCus_id_fk());
				if( ret_wh_mtrl_all != null){
					ret_wh_mtrl.setMtrl_qty(ret_wh_mtrl_all.getMtrl_qty()+ret_wh_mtrl.getEvt_qty());
					ret_wh_mtrl.setDest_shop(dest_shop);
					ret_wh_mtrl.setIni_dest_shop(dest_shop);
					ret_wh_mtrl.setEvt_cate("CHGY");//辅料收货
					ret_wh_mtrl.setMtrl_evt_cate("CHGY");
					ret_wh_mtrl.setEvt_usr(evt_user);
					ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
					ret_wh_mtrl.setInwh_timestamp(gEvtTimestamp);
					ret_wh_mtrl.setMtrl_note(null);
					retWhMtrlDao.update(ret_wh_mtrl);
					ret_wh_mtrl_all.setMtrl_qty(null );
					retWhMtrlDao.update(ret_wh_mtrl_all);
				}else{
					ret_wh_mtrl.setMtrl_qty(ret_wh_mtrl.getEvt_qty());
					ret_wh_mtrl.setEvt_cate("CHGY");//辅料收货
					ret_wh_mtrl.setMtrl_evt_cate("CHGY");
					ret_wh_mtrl.setIni_dest_shop(dest_shop);
					ret_wh_mtrl.setDest_shop(dest_shop);
					ret_wh_mtrl.setEvt_usr(evt_user);
					ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
					ret_wh_mtrl.setInwh_timestamp(gEvtTimestamp);
					ret_wh_mtrl.setMtrl_note(null);
					retWhMtrlDao.update(ret_wh_mtrl);
				}
			}
		
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
	}
	
	public void getWhMtrl4700Proc(){
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		try{
			XpwhmtrlIA iary = inTrx.getIary().get(0);
			String mtrl_prod_id = iary.getMtrl_prod_id_fk();
			String cus_id_fk = iary.getCus_id();
			String mtrl_cate = iary.getMtrl_cate();
			String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
			String dest_shop = iary.getDest_shop();
			String sql = "SELECT DISTINCT MTRL_PROD_ID_FK FROM RET_WH_MTRL A JOIN BIS_MTRL B ON A.MTRL_PROD_ID_FK=B.MTRL_PROD_ID WHERE 1=1";
			
	        if(!Util.isSpaceCheck(cus_id_fk)){
	        	sql = sql + " AND A.CUS_ID_FK='" + cus_id_fk + "'";
	        }
			if(!Util.isSpaceCheck(mtrl_cate)){
				sql = sql + " AND A.MTRL_CATE='" + mtrl_cate +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_id)){
				sql = sql + " AND A.MTRL_PROD_ID_FK='" + mtrl_prod_id +"'";
			}
			if(!Util.isSpaceCheck(dest_shop)){
				sql = sql + " AND A.DEST_SHOP='" + dest_shop +"'";
			}
			if(!Util.isSpaceCheck(mtrl_prod_dsc)){
				sql = sql + " AND B.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
			}

			List<String> mtrlList = retWhMtrlDao.findBySQL(sql);
			for(int i = 0; i < mtrlList.size(); i++){
				String mtrlInfo = mtrlList.get(i);
				XpwhmtrlOA oary = new XpwhmtrlOA();
				oary.setMtrl_prod_id_fk(mtrlInfo);
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
	
	public void delRetMtrlProc(){
		String mtrl_prod_id="",mtrl_seq_id="",evt_user="",dest_shop="",cus_id="";
		mtrl_seq_id = inTrx.getIary().get(0).getMtrl_seq_id();
		mtrl_prod_id = inTrx.getIary().get(0).getMtrl_prod_id_fk();
		evt_user = inTrx.getEvt_usr();
		double mtrl_qty = Double.valueOf( inTrx.getIary().get(0).getEvt_qty() );
		
		Ret_mtrl ret_mtrl = retMtrlDao.get(mtrl_seq_id);
		if( ret_mtrl == null){
			gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做过发料,无法做异常退料！";
			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}
		if( mtrl_qty > ret_mtrl.getMtrl_qty()){
			gszMsgBuf = "物料["+mtrl_prod_id+"]退料的数量大于已发料数量，无法退料";
			xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
			return;
		}else if( mtrl_qty == ret_mtrl.getMtrl_qty()){
			ret_mtrl.setEvt_cate("FLTO");//辅料发料退料
			ret_mtrl.setEvt_usr(evt_user);
			ret_mtrl.setEvt_timestamp(gEvtTimestamp);
        	retMtrlDao.delete(ret_mtrl);
    	    
    		dest_shop = ret_mtrl.getDest_shop();
    		cus_id = ret_mtrl.getCus_id_fk();
    		double evt_qty = mtrl_qty;
    		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
    		if( bis_mtrl == null){
    			gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
    			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
    			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
    			return;
    		}
    		
    		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
    		
    		Ret_wh_mtrl ret_wh_mtrl = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, mtrl_prod_id,dest_shop,cus_id);
    		if( ret_wh_mtrl != null){
    			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
    			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
    			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
    			ret_wh_mtrl_new.setMtrl_qty(ret_wh_mtrl.getMtrl_qty()+evt_qty);
    			ret_wh_mtrl_new.setEvt_qty(evt_qty);
    			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
    			ret_wh_mtrl_new.setDest_shop(dest_shop);
    			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
    			ret_wh_mtrl_new.setEvt_cate("FLTO");//发料退料
    			ret_wh_mtrl_new.setMtrl_evt_cate("FLTO");
    			ret_wh_mtrl_new.setEvt_usr(evt_user);
    			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setCus_id_fk(cus_id);
    			retWhMtrlDao.save(ret_wh_mtrl_new);
    			ret_wh_mtrl.setMtrl_qty(null );
    			retWhMtrlDao.update(ret_wh_mtrl);
    		}else{
    			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
    			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
    			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
    			ret_wh_mtrl_new.setMtrl_qty(evt_qty);
    			ret_wh_mtrl_new.setEvt_qty(evt_qty);
    			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
    			ret_wh_mtrl_new.setEvt_cate("FLTO");
    			ret_wh_mtrl_new.setMtrl_evt_cate("FLTO");
    			ret_wh_mtrl_new.setDest_shop(dest_shop);
    			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
    			ret_wh_mtrl_new.setEvt_usr(evt_user);
    			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setCus_id_fk(cus_id);
    			retWhMtrlDao.save(ret_wh_mtrl_new);
    		}
        }else{
        	ret_mtrl.setMtrl_qty(ret_mtrl.getMtrl_qty() - mtrl_qty);
        	ret_mtrl.setEvt_cate("FLTO");//辅料发料退料
        	ret_mtrl.setEvt_usr(evt_user);
        	ret_mtrl.setEvt_timestamp(gEvtTimestamp);
    	    retMtrlDao.update(ret_mtrl);
        	
    		dest_shop = ret_mtrl.getDest_shop();
    		cus_id = ret_mtrl.getCus_id_fk();
    		double evt_qty = mtrl_qty;
    		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
    		if( bis_mtrl == null){
    			gszMsgBuf = "此物料["+mtrl_prod_id+"]没有做基础维护,无法入库！";
    			xpwhmtrlO.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
    			xpwhmtrlO.setRtn_mesg(gszMsgBuf);
    			return;
    		}
    		
    		String hql = " FROM Ret_wh_mtrl WHERE MTRL_PROD_ID_FK=? AND DEST_SHOP=? AND CUS_ID_FK=? AND MTRL_QTY<>'null'";
    		
    		Ret_wh_mtrl ret_wh_mtrl = (Ret_wh_mtrl) retWhMtrlDao.uniqueResult(hql, mtrl_prod_id,dest_shop,cus_id);
    		if( ret_wh_mtrl != null){
    			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
    			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
    			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
    			ret_wh_mtrl_new.setMtrl_qty(ret_wh_mtrl.getMtrl_qty()+evt_qty);
    			ret_wh_mtrl_new.setEvt_qty(evt_qty);
    			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
    			ret_wh_mtrl_new.setDest_shop(dest_shop);
    			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
    			ret_wh_mtrl_new.setEvt_cate("FLTO");//发料退料
    			ret_wh_mtrl_new.setMtrl_evt_cate("FLTO");
    			ret_wh_mtrl_new.setEvt_usr(evt_user);
    			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setCus_id_fk(cus_id);
    			retWhMtrlDao.save(ret_wh_mtrl_new);
    			ret_wh_mtrl.setMtrl_qty(null );
    			retWhMtrlDao.update(ret_wh_mtrl);
    		}else{
    			Ret_wh_mtrl ret_wh_mtrl_new = new Ret_wh_mtrl();
    			ret_wh_mtrl_new.setMtrl_seq_id(gEvtSeqID);
    			ret_wh_mtrl_new.setMtrl_prod_id_fk(mtrl_prod_id);
    			ret_wh_mtrl_new.setMtrl_qty(evt_qty);
    			ret_wh_mtrl_new.setEvt_qty(evt_qty);
    			ret_wh_mtrl_new.setMtrl_cate(bis_mtrl.getMtrl_cate());
    			ret_wh_mtrl_new.setEvt_cate("FLTO");
    			ret_wh_mtrl_new.setMtrl_evt_cate("FLTO");
    			ret_wh_mtrl_new.setDest_shop(dest_shop);
    			ret_wh_mtrl_new.setIni_dest_shop(dest_shop);
    			ret_wh_mtrl_new.setEvt_usr(evt_user);
    			ret_wh_mtrl_new.setEvt_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setInwh_timestamp(gEvtTimestamp);
    			ret_wh_mtrl_new.setCus_id_fk(cus_id);
    			retWhMtrlDao.save(ret_wh_mtrl_new);
    		}
        }

		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		
	}
	
	
	public void inqChgMtrlProc(){
		List<XpwhmtrlOA> oaryList = new ArrayList<XpwhmtrlOA>();
		try{
			XpwhmtrlIA iary = inTrx.getIary().get(0);
			String mtrl_prod_id = iary.getMtrl_prod_id_fk();
			String cus_id_fk = iary.getCus_id();
			String sql = "SELECT A.* FROM RET_WH_MTRL A WHERE 1=1";
	        if(!Util.isSpaceCheck(cus_id_fk)){
	        	sql = sql + " AND A.CUS_ID_FK='" + cus_id_fk + "'";
	        }
			if(!Util.isSpaceCheck(mtrl_prod_id)){
				sql = sql + " AND A.MTRL_PROD_ID_FK='" + mtrl_prod_id +"'";
			}
			sql = sql + " AND A.MTRL_NOTE<>'null' ORDER BY MTRL_PROD_ID_FK,DEST_SHOP,CUS_ID_FK";

			List<Ret_wh_mtrl> mtrlList = retWhMtrlDao.findBySQL(sql, Ret_wh_mtrl.class);
			for(Ret_wh_mtrl ret_wh_mtrl:mtrlList){
				XpwhmtrlOA oary = new XpwhmtrlOA();
				oary.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
				oary.setMtrl_qty(ret_wh_mtrl.getMtrl_qty());
				oary.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
				oary.setEvt_timestamp(ret_wh_mtrl.getEvt_timestamp());
				oary.setInwh_timestamp(ret_wh_mtrl.getInwh_timestamp());
				oary.setEvt_usr(ret_wh_mtrl.getEvt_usr());
				oary.setDest_shop(ret_wh_mtrl.getDest_shop());
				oary.setCus_id(ret_wh_mtrl.getCus_id_fk());
				Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wh_mtrl.getMtrl_prod_id_fk());
				String hql = "FROM Bis_data where data_cate=? and data_item=?";
				Bis_data bis_data = bisDataDao.uniqueResult(hql, "CUSD",ret_wh_mtrl.getCus_id_fk());
				if(null!=bis_data){
					oary.setCus_dsc(bis_data.getData_ext());
				}else{
					oary.setCus_dsc(ret_wh_mtrl.getCus_id_fk());
				}
				oary.setMtrl_prod_dsc(bis_mtrl.getMtrl_prod_dsc());
				oary.setEvt_qty(ret_wh_mtrl.getEvt_qty());
				oary.setMtrl_seq_id(ret_wh_mtrl.getMtrl_seq_id());
				String	dest_shop1=new StringBuilder(ret_wh_mtrl.getDest_shop()).reverse().toString(); 
				String hql1 = "FROM Bis_data WHERE data_cate = 'DEST' AND data_ext = 'A' AND data_id = '" + dest_shop1 + "'"; 
				List<Bis_data> dataList = bisDataDao.find(hql1);
				if(dataList.size() == 0){
					gszMsgBuf = "箱子["+ret_wh_mtrl.getMtrl_lot_id()+"]仓别["+ dest_shop1 +"]在bis_data中不存在";
					xpwhmtrlO.setRtn_code(E_RET_MTRL + SQL_OTHER_ERROR+"");
					xpwhmtrlO.setRtn_mesg(gszMsgBuf);
					return;
				}
				
				oary.setDes_data_desc(dataList.get(0).getData_desc());
				oaryList.add(oary);
			}
		}catch(Exception ex){
			xpwhmtrlO.setRtn_code(E_XPBISMTR_OTHER_ERR +"");
			xpwhmtrlO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpwhmtrlO.setRtn_code( RETURN_CODE_OK );
		xpwhmtrlO.setRtn_mesg( RETURN_MESG_OK );
		xpwhmtrlO.setOary(oaryList);
		xpwhmtrlO.setTbl_cnt(oaryList.size());
	}
}
